package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.dto.NumDTO;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbPowerCircleBo;
import org.dromara.system.domain.vo.WbPowerCircleVo;
import org.dromara.system.domain.WbPowerCircle;
import org.dromara.system.mapper.WbPowerCircleMapper;
import org.dromara.system.service.IWbPowerCircleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 电力圈增删改查Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class WbPowerCircleServiceImpl implements IWbPowerCircleService {

    private final WbPowerCircleMapper baseMapper;

    /**
     * 查询电力圈增删改查
     */
    @Override
    public WbPowerCircleVo queryById(Long circleId){
        return baseMapper.selectVoById(circleId);
    }

    /**
     * 查询电力圈列表
     */
    @Override
    public TableDataInfo<WbPowerCircleVo> queryPageList(WbPowerCircleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbPowerCircle> lqw = buildQueryWrapper(bo);

        Page<WbPowerCircleVo> result = baseMapper.selectVoPageWithProvince(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电力圈增删改查列表
     */
    @Override
    public List<WbPowerCircleVo> queryList(WbPowerCircleBo bo) {
        LambdaQueryWrapper<WbPowerCircle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbPowerCircle> buildQueryWrapper(WbPowerCircleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbPowerCircle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WbPowerCircle::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), WbPowerCircle::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getPictureUrl()), WbPowerCircle::getPictureUrl, bo.getPictureUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getNewsType()), WbPowerCircle::getNewsType, bo.getNewsType());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), WbPowerCircle::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), WbPowerCircle::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), WbPowerCircle::getContacts, bo.getContacts());
        lqw.eq(bo.getReleaseTime() != null, WbPowerCircle::getReleaseTime, bo.getReleaseTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReleaseType()), WbPowerCircle::getReleaseType, bo.getReleaseType());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()),WbPowerCircle::getTitle,bo.getTitle());
        lqw.orderByDesc(WbPowerCircle::getReleaseTime);
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WbPowerCircle::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增电力圈增删改查
     */
    @Override
    public Boolean insertByBo(WbPowerCircleBo bo) {
        WbPowerCircle add = MapstructUtils.convert(bo, WbPowerCircle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCircleId(add.getCircleId());
        }
        return flag;
    }

    /**
     * 修改电力圈增删改查
     */
    @Override
    public Boolean updateByBo(WbPowerCircleBo bo) {
        WbPowerCircle update = MapstructUtils.convert(bo, WbPowerCircle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbPowerCircle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电力圈增删改查
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<WbPowerCircleVo> queryPageListZYT( Long userId,PageQuery pageQuery) {
        Page<WbPowerCircleVo> result = baseMapper.selectVoPageWithProvinceZYT(pageQuery.build(),userId);
        return TableDataInfo.build(result);
    }

    @Override
    public List<NumDTO> countNum() {
        return baseMapper.selectCountNum();
    }
}
